package com.byl.mvvm

import android.app.Activity
import android.content.Context
import android.content.Intent
import androidx.activity.result.contract.ActivityResultContract
import androidx.recyclerview.widget.RecyclerView
import com.byl.mvvm.ui.main.SecondActivity
import com.byl.mvvm.ui.main.TestEventActivity

/**
 *
 * @ProjectName:    MVVM
 * @Package:        com.byl.mvvm
 * @ClassName:      CustomActivityResultContract
 * @Description: 替换activityresult
 * @Author:  lirui
 * @CreateDate:     2020/11/16 0016 上午 10:55
 */
class CustomActivityResultContract : ActivityResultContract<String, String>() {

    override fun createIntent(context: Context, input: String?): Intent {
        //要传到下一个activity中的数据，从A activity中带到B activity中的数据。
        return Intent(context, TestEventActivity::class.java).putExtra("name", input)
    }

    override fun parseResult(resultCode: Int, intent: Intent?): String {
        //从activity回传的数据
        val result = intent?.getStringExtra("result") //从B activity中回传的数据。
        if (resultCode == Activity.RESULT_OK && result != null) {
            return "$result"
        }
        return ""
    }
}